package com.aikon.wht.multiThread;

import com.google.common.util.concurrent.*;

import java.util.concurrent.*;

/**
 * @author haitao.wang
 */
public class TestOfListeningDecorator {

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new SynchronousQueue<>());

        Thread t = new Thread(){
            @Override
            public void run() {
                ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(threadPoolExecutor);
                Callable c1 = new Callable() {
                    @Override
                    public Object call() throws Exception {
                        System.out.println("c1 exec");
                        return 1;
                    }
                };
                Callable c2 = new Callable() {
                    @Override
                    public Object call() throws Exception {
                        System.out.println("c2 exec");
                        return 2;
                    }
                };

                ListenableFuture listenableFuture1 = listeningExecutorService.submit(c1);
                try {
                    System.out.println(listenableFuture1.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                listenableFuture1.addListener(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("c1 end");
                    }
                },MoreExecutors.directExecutor());
                listenableFuture1.addListener(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("c11 end");
                    }
                },MoreExecutors.directExecutor());
                ListenableFuture listenableFuture2 =  listeningExecutorService.submit(c2);
                try {
                    System.out.println(listenableFuture2.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }

                Futures.addCallback(listenableFuture1, new FutureCallback<Object>() {
                    @Override
                    public void onSuccess(Object o) {
                        System.out.println("C1 result" + o);
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        System.out.println(throwable.getMessage());

                    }
                },MoreExecutors.directExecutor());

                listenableFuture2.addListener(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("c2 end");
                    }
                },MoreExecutors.directExecutor());

            }
        };
        t.start();
        t.join();



        System.out.println(threadPoolExecutor.getTaskCount());
        System.out.println(threadPoolExecutor.getActiveCount());
        System.out.println(threadPoolExecutor.getCompletedTaskCount());
        System.out.println(threadPoolExecutor.getCorePoolSize());
        System.out.println(threadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS));
        System.out.println(threadPoolExecutor.getLargestPoolSize());
        System.out.println(threadPoolExecutor.getMaximumPoolSize());
        System.out.println(threadPoolExecutor.getPoolSize());
        System.out.println(threadPoolExecutor.getQueue().size());
        System.out.println(threadPoolExecutor.getRejectedExecutionHandler().getClass());
        System.out.println(threadPoolExecutor.getThreadFactory().getClass());

    }
}
